-
Quotes from Bash Reference Manual: Arrays
Any element of an array may be referenced using ${name[subscript]}. The braces are required to avoid conflicts with the shell's filename expansion operators. If the subscript is ‘@’ or ‘*’, the word expands to all members of the array name. These subscripts differ only when the word appears within double quotes. If the word is double-quoted, ${name[*]} expands to a single word with the value of each array member separated by the first character of the IFS variable, and ${name[@]} expands each element of name to a separate word. When there are no array members, ${name[@]} expands to nothing. If the double-quoted expansion occurs within a word, the expansion of the first parameter is joined with the beginning part of the original word, and the expansion of the last parameter is joined with the last part of the original word. This is analogous to the expansion of the special parameters ‘@’ and ‘*’. ${#name[subscript]} expands to the length of ${name[subscript]}. If subscript is ‘@’ or ‘*’, the expansion is the number of elements in the array. Referencing an array variable without a subscript is equivalent to referencing with a subscript of 0.
-
append
a=(1 0 7 3) echo ${a[@]} #1 0 7 3 b=(${a[@]} 8) echo ${b[@]} #1 0 7 3 8
-
slice
a=(1 0 7 3) echo ${a[0]} echo ${a[@]:1} # from the 2nd till the end
-
get a list of file names
names=`ls ./` for name in $names; do echo $name; done
-
convert string to array
str='1 2 3 a b c' array=($str)
-
or
dd="random normal X1 link"; for d in $dd; do echo $d; done;
- Join elements of an array
http://stackoverflow.com/questions/1527049/bash-join-elements-of-an-array
FOO=( a b c ) SAVE_IFS=$IFS IFS="," FOOJOIN="${FOO[*]}" IFS=$SAVE_IFS echo $FOOJOIN
#/!bin/bash foo=('foo bar' 'foo baz' 'bar baz') bar=$(printf ",%s" "${foo[@]}") bar=${bar:1} echo $bar
reference: The Ultimate Bash Array Tutorial with 15 Examples
Hide Comments